package com.fywsy.fyweb.strategy;

import com.fywsy.fyweb.entity.SendItem;
import com.fywsy.fyweb.utils.RestTemplateUtil;
import com.fywsy.fyweb.utils.TextUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;
import java.util.Map;

/**
 * 大话-精卫填海-渡劫
 *
 * 游戏后台实现类
 * 注解gameId对应数据库游戏id
 * 注解GongnengMethod对应数据库功能id
 */
@Component
@GameId(22)
public class DHHaiziGameStrategy extends BaseGameStrategy {

    public static final String LOGIN = "http://114.66.45.129:30298/login";
    public static final String SEND_COIN = "http://114.66.45.129:30298/system/send/coin";

    public static final String PAY = "http://114.66.45.129:30298/system/send/recharge";

    public static final String MAIL = "http://114.66.45.129:30298/system/send/prop";
    public static final String EXP = "http://114.66.45.129:30298/system/send/exp";

    public static final String USERNAME = "admin";

    public static final String PASSWORD = "123456";

    private static String jsessionId;
    private static String rememberMe;
    private static long tokenExpireTime;

    @GongnengMethod(2201)
    public String coin(SendItem item) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("type", "1");
        params.add("serverSerial", "103");

        params.add("name", item.uid);
        params.add("serial", item.itemId); // 这里需要根据实际情况设置
        params.add("num", item.number);

        String response = RestTemplateUtil.sendPostFormAndGetJsonKey(SEND_COIN, params, "msg", getHeader());
        if(!TextUtils.isEmpty(response)) {
            return response;
        }
        return "发送失败";
    }

    /**
     * http://110.40.83.197:8116/system/send/recharge
     * name: 10001568
     * serverSerial: 103
     * serial: 1006
     * num: 11
     * @param item
     * @return
     */
    @GongnengMethod(2202)
    public String pay(SendItem item) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("serverSerial", "103");

        params.add("name", item.uid);
        params.add("serial", item.itemId); // 这里需要根据实际情况设置
        params.add("num", item.number);

        String response = RestTemplateUtil.sendPostFormAndGetJsonKey(PAY, params, "msg", getHeader());
        if(!TextUtils.isEmpty(response)) {
            return response;
        }
        return "发送失败";
    }


    /**
     * name: 10001568
     * serverSerial: 103
     * serial: 10001
     * num: 11
     * @param item
     * @return
     */
    @GongnengMethod(2203)
    public String wupin(SendItem item) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("serverSerial", "103");

        params.add("name", item.uid);
        params.add("serial", item.itemId); // 这里需要根据实际情况设置
        params.add("num", item.number);

        String response = RestTemplateUtil.sendPostFormAndGetJsonKey(MAIL, params, "msg", getHeader());
        if(!TextUtils.isEmpty(response)) {
            return response;
        }
        return "发送失败";
    }


    /**
     * name: 1061
     * serverSerial: 102
     * num: 1223
     * @param item
     * @return
     */
    @GongnengMethod(2204)
    public String exp(SendItem item) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("serverSerial", "103");

        params.add("name", item.uid);
        params.add("num", item.number);

        String response = RestTemplateUtil.sendPostFormAndGetJsonKey(EXP, params, "msg", getHeader());
        if(!TextUtils.isEmpty(response)) {
            return response;
        }
        return "发送失败";
    }



    private MultiValueMap<String, String> getHeader() {
        if (jsessionId == null || System.currentTimeMillis() >= tokenExpireTime) {
            login();
        }
        MultiValueMap<String, String> header = new LinkedMultiValueMap<>();
        header.add("Cookie", "JSESSIONID=" + jsessionId + "; rememberMe=" + rememberMe);
        header.add("accept", "*/*");
        header.add("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        header.add("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        header.add("x-requested-with", "XMLHttpRequest");
        return header;
    }

    public String login() {
        Map<String,String> params = new HashMap<>();
        params.put("username", USERNAME);
        params.put("password", PASSWORD);
        params.put("rememberMe", "true");

        Map<String, String> responseHeaders = RestTemplateUtil.sendPostFormAndGetHeaders(LOGIN, params);

        if (responseHeaders != null) {
            String setCookie = responseHeaders.get("Set-Cookie");
            if (setCookie != null) {
                String[] cookies = setCookie.split(";");
                for (String cookie : cookies) {
                    if (cookie.startsWith("JSESSIONID=")) {
                        jsessionId = cookie.substring("JSESSIONID=".length());
                    } else if (cookie.startsWith("rememberMe=")) {
                        rememberMe = cookie.substring("rememberMe=".length());
                    }
                }
                // 设置token过期时间为30min
                tokenExpireTime = System.currentTimeMillis() + 1 * 60 * 1000;
                return "登录成功";
            }
        }
        return "登录失败";
    }


    public static void main(String[] args) {
        SendItem item = new SendItem();
        item.uid = "10002148";
//        item.itemId = "10001";
        item.number = "12311";
        String coin = new DHHaiziGameStrategy().exp(item);
        System.out.println(coin);
    }
}

